using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace One_Crappy_RPG
{
    public class ScreenManager : DrawableGameComponent
    {
        public List<GameScreen> gameScreen = new List<GameScreen>();
        List<GameScreen> screensToUpdate = new List<GameScreen>();

        public IGraphicsDeviceService graphicsDeviceService;
        public GraphicsDeviceManager graphics;
        public ContentManager content;
        public SpriteBatch spriteBatch;
        public SpriteFont font;

        /// <summary>
        /// Expose access to our Game instance (this is protected in the
        /// default GameComponent, but we want to make it public).
        /// </summary>
        new public Game Game
        {
            get { return base.Game; }
        }

        /// <summary>
        /// Expose access to our graphics device (this is protected in the
        /// default DrawableGameComponent, but we want to make it public).
        /// </summary>
        new public GraphicsDevice GraphicsDevice
        {
            get { return base.GraphicsDevice; }
        }

        public ScreenManager(Game game)
            : base(game)
        {
            content = new ContentManager(game.Services);

            graphicsDeviceService = (IGraphicsDeviceService)game.Services.GetService(
                                                        typeof(IGraphicsDeviceService));

            if (graphicsDeviceService == null)
                throw new InvalidOperationException("No graphics device service.");
        }

        protected override void LoadContent()
        {
            // Load content belonging to the screen manager.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            content.RootDirectory = "Content";

            font = content.Load<SpriteFont>("Arial");
            InputManager.Font = font;

            // Tell each of the screens to load their content.
            foreach (GameScreen screen in gameScreen)
            {
                screen.LoadContent();
            }
        }

        protected override void UnloadContent()
        {
            // Unload content belonging to the screen manager.
            content.Unload();

            // Tell each of the screens to unload their content.
            foreach (GameScreen screen in gameScreen)
            {
                screen.UnloadContent();
            }
        }



        public override void Update(GameTime gameTime)
        {
            // Make a copy of the master screen list, to avoid confusion if
            // the process of updating one screen adds or removes others.
            screensToUpdate.Clear();

            foreach (GameScreen screen in gameScreen)
            {
                screensToUpdate.Add(screen);
            }

            foreach (GameScreen screen in screensToUpdate)
            {
                if (screen.isActive == true)
                {
                    screen.Update(gameTime);
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (GameScreen screen in gameScreen)
            {
                if (screen.isActive == true)
                {
                    screen.Draw(gameTime);
                }
            }
        }

        public void AddScreen(GameScreen screen)
        {
            screen.ScreenManager = this;

            if ((graphicsDeviceService != null) &&
                (graphicsDeviceService.GraphicsDevice != null))
            {
                screen.LoadContent();
            }
            gameScreen.Add(screen);
        }

        public void RemoveScreen(GameScreen screen)
        {
            // If we have a graphics device, tell the screen to unload content.
            if ((graphicsDeviceService != null) &&
                (graphicsDeviceService.GraphicsDevice != null))
            {
                screen.UnloadContent();
            }
            gameScreen.Remove(screen);
        }
    }
}
